package com.example.android427.Fragment;



import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;

import com.example.android427.R;

import java.util.ArrayList;
import java.util.List;

public class qikanFragment2 extends ListFragment {
    List<geren> qikanfenleiList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        qikanfenleiList = new ArrayList<>();
        init();
        list_wechat_adapter adapter = new list_wechat_adapter(getContext(), R.layout.qikanfenlei, qikanfenleiList);
        this.setListAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.qikanfragment2, container, false);
    }

    private void init() {
        geren a = new geren("人文科学", R.drawable.qikanfenleia, R.drawable.right_jkh);
        qikanfenleiList.add(a);
        geren b = new geren("政治时事", R.drawable.qikanfenleib, R.drawable.right_jkh);
        qikanfenleiList.add(b);
        geren c = new geren("医学保健", R.drawable.qikanfenleic, R.drawable.right_jkh);
        qikanfenleiList.add(c);
        geren d = new geren("通信科技", R.drawable.qikanfenleid, R.drawable.right_jkh);
        qikanfenleiList.add(d);
        geren e = new geren("商业财经", R.drawable.qikanfenleie, R.drawable.right_jkh);
        qikanfenleiList.add(e);
        geren f = new geren("生活休闲", R.drawable.qikanfenleif, R.drawable.right_jkh);
        qikanfenleiList.add(f);
        geren g = new geren("艺术传媒", R.drawable.qikanfenleig, R.drawable.right_jkh);
        qikanfenleiList.add(g);
    }

    public class geren {
        private String name;
        private int imageId;
        private int imageId1;

        public geren(String name, int imageId, int imageId1) {
            this.name = name;
            this.imageId = imageId;
            this.imageId1 = imageId1;
        }

        public String getName() {
            return name;
        }

        public int getimageId() {
            return imageId;
        }

        public int getimageId1() {
            return imageId1;
        }
    }

    public class list_wechat_adapter extends ArrayAdapter<geren> {
        private int resourceId;

        public list_wechat_adapter(@NonNull Context context, int resource, @NonNull List<geren> objects) {
            super(context, resource, objects);
            resourceId = resource;
        }


        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            geren w = getItem(position);//获取对象
            View view = LayoutInflater.from(getContext()).inflate(resourceId, null);

            ImageView img1 = view.findViewById(R.id.geren_img1);
            img1.setImageResource(w.getimageId());
            TextView t1 = view.findViewById(R.id.geren_t1);
            t1.setText(w.getName());
            ImageView img4 = view.findViewById(R.id.geren_img2);
            img4.setImageResource(w.getimageId1());
            return view;
        }
    }
}




